﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Text;

namespace HTTPDebuggerTools.com
{
    public partial class Base64EncodeDecode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NoBotState state;
            if (!NoBot1.IsValid(out state))
                Response.Redirect("http://www.httpdebugger.com/");
        }

        protected void EncodeBtn_Click(object sender, EventArgs e)
        {
            ResultBox.Visible = false;
            ErrorLabel.Visible = false;

            string src = SourceBox.Text.Trim();
            if (src.Length != 0)
            {
                try
                {
                    byte[] toEncodeAsBytes = ASCIIEncoding.ASCII.GetBytes(src);

                    ResultBox.Text = Convert.ToBase64String(toEncodeAsBytes);
                    ResultBox.Visible = true;
                }
                catch
                {
                    ErrorLabel.Text = "Error occurred while encoding to Base64.";
                    ErrorLabel.Visible = true;
                }
            }
        }

        protected void DecodeBtn_Click(object sender, EventArgs e)
        {
            ResultBox.Visible = false;
            ErrorLabel.Visible = false;

            string src = SourceBox.Text.Trim();
            if (src.Length != 0)
            {
                try
                {
                    byte[] encodedDataAsBytes = Convert.FromBase64String(src);

                    ResultBox.Text = ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
                    ResultBox.Visible = true;
                }
                catch
                {
                    ErrorLabel.Text = "Error occurred while decoding from Base64.";
                    ErrorLabel.Visible = true;
                }
            }
        }
    }
}
